<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
		:close-on-press-escape='false' :fullscreen='true'>
		<el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
			label-width="120px">

			<el-form-item label="批号" prop="lot">
				<el-input v-model="dataForm.lot" placeholder="批号"></el-input>
			</el-form-item>
			<el-form-item label="物料编码" prop="sku">
				<el-input v-model="dataForm.sku" placeholder="物料编码"></el-input>
			</el-form-item>
			<el-form-item label="生产日期" prop="lot1">
				<el-input v-model="dataForm.lot1" placeholder="生产日期"></el-input>
			</el-form-item>
			<el-form-item label="有效日期" prop="lot2">
				<el-input v-model="dataForm.lot2" placeholder="有效日期"></el-input>
			</el-form-item>
			<el-form-item label="是否加长" prop="lot3">
				<el-input v-model="dataForm.lot3" placeholder="是否加长"></el-input>
			</el-form-item>
			<el-form-item label="价格" prop="lot4">
				<el-input v-model="dataForm.lot4" placeholder="价格"></el-input>
			</el-form-item>
			<el-form-item label="部门/供应商" prop="lot5">
				<el-input v-model="dataForm.lot5" placeholder="部门/供应商"></el-input>
			</el-form-item>
			<el-form-item label="计划单号/工序号" prop="lot6">
				<el-input v-model="dataForm.lot6" placeholder="计划单号/工序号"></el-input>
			</el-form-item>
			<el-form-item label="主制车间" prop="lot7">
				<el-input v-model="dataForm.lot7" placeholder="主制车间"></el-input>
			</el-form-item>
			<el-form-item label="任务编码" prop="lot8">
				<el-input v-model="dataForm.lot8" placeholder="任务编码"></el-input>
			</el-form-item>
			<el-form-item label="新旧" prop="lot9">
				<el-input v-model="dataForm.lot9" placeholder="新旧"></el-input>
			</el-form-item>
			<el-form-item label="作废" prop="lot10">
				<el-input v-model="dataForm.lot10" placeholder="作废"></el-input>
			</el-form-item>
			<el-form-item label="炉批号" prop="lot11">
				<el-input v-model="dataForm.lot11" placeholder="炉批号"></el-input>
			</el-form-item>
			<el-form-item label="材料复验号" prop="lot12">
				<el-input v-model="dataForm.lot12" placeholder="材料复验号"></el-input>
			</el-form-item>
			<el-form-item label="牌号" prop="lot13">
				<el-input v-model="dataForm.lot13" placeholder="牌号"></el-input>
			</el-form-item>
			<el-form-item label="属性14" prop="lot14">
				<el-input v-model="dataForm.lot14" placeholder="属性14"></el-input>
			</el-form-item>
			<el-form-item label="属性15" prop="lot15">
				<el-input v-model="dataForm.lot15" placeholder="属性15"></el-input>
			</el-form-item>
			<el-form-item label="更新人" prop="updatuser">
				<el-input v-model="dataForm.updatuser" placeholder="更新人"></el-input>
			</el-form-item>
			<el-form-item label="更新时间" prop="updatetime">
				<el-input v-model="dataForm.updatetime" placeholder="更新时间"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		getLotById,
		putLot,
		postLot
	} from "@/network/lswms/stock/Lot"
	import {
		ElMessage
	} from 'element-plus'
	import _ from 'lodash'


	export default {
		name: "lotAddOrUpdate",
		emits: ["refreshDataList"],
		setup(props, {
			emit
		}) {
			const visible = ref(false);
			const dataForm = ref({
				id: '',
				lot: '',
				sku: '',
				lot1: '',
				lot2: '',
				lot3: '',
				lot4: '',
				lot5: '',
				lot6: '',
				lot7: '',
				lot8: '',
				lot9: '',
				lot10: '',
				lot11: '',
				lot12: '',
				lot13: '',
				lot14: '',
				lot15: '',
				adduser: '',
				addtime: '',
				updatuser: '',
				updatetime: ''
			});


			const dataRule = reactive({
				lot: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				sku: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot1: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot2: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot3: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot4: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot5: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot6: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot7: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot8: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot9: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot10: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot11: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot12: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot13: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot14: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				lot15: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				/*        updater: [
				          { required: true, message: '必填项不能为空', trigger: 'blur' }
				        ],          updateDate: [
				          { required: true, message: '必填项不能为空', trigger: 'blur' }
				        ]  */
			})

			const dataFormRef = ref(null)

			const init = () => {
				visible.value = true;
				nextTick(function() {
					dataFormRef.value.resetFields();
					if (dataForm.value.id) {
						getlotInfo();
					}

				})
			}

			const getlotInfo = () => {
				getLotById(dataForm.value.id).then(function(res) {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value = _.assignIn({}, dataForm.value, res.data);
				}).catch((err) => {});
			}

			// 表单提交
			const dataFormSubmitHandle = _.debounce(() => {
				dataFormRef.value.validate(function(valid) {
					if (!valid) {
						return false;
					}

					let qing = () => {
						return !dataForm.value.id ? postLot(dataForm.value) : putLot(dataForm.value)
					}
					qing().then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								visible.value = false
								emit('refreshDataList')
							}
						})
					}).catch(function() {});

				})
			}, 1000, {
				'leading': true,
				'trailing': false
			})

			return {
				visible,
				dataForm,
				dataRule,
				init,
				dataFormSubmitHandle,
				dataFormRef

			}
		},


	}
</script>

<style scoped>

</style>
